package com.rc.saas.mini.service.rc;

import com.rc.saas.mini.common.base.BaseMapper;
import com.rc.saas.mini.mapper.rc.RcNoticeNotificationMapper;
import com.rc.saas.mini.model.member.MemberInfo;
import com.rc.saas.mini.model.order.OrderInfo;
import com.rc.saas.mini.model.rc.RcNoticeNotification;
import com.rc.saas.mini.model.rc.RcNoticeNotificationExample;
import com.rc.saas.mini.model.tenant.TenantNotice;
import com.rc.saas.mini.service.member.MemberInfoService;
import com.rc.saas.mini.service.order.OrderInfoService;
import com.rc.saas.mini.service.tenant.TenantNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RcNoticeNotificationServiceImpl implements RcNoticeNotificationService {
    @Autowired
    private RcNoticeNotificationMapper rcNoticeNotificationMapper;

    @Autowired
    private MemberInfoService memberInfoService;

    @Autowired
    private TenantNoticeService tenantNoticeService;

    @Autowired
    private OrderInfoService orderInfoService;

    @Override
    public RcNoticeNotificationMapper getMapper() {
        return rcNoticeNotificationMapper;
    }


    @Override
    public void addNotice(Long memberId) {
        //1.查询当前所有用户数据
        MemberInfo memberInfo = memberInfoService.selectByPrimaryKey(memberId);

        Date date = new Date();
        if (memberInfo != null) {
            date = memberInfo.getLastNotityTime() == null ? memberInfo.getCreatedTime() : memberInfo.getLastNotityTime();
            if (date == null) {
                date = memberInfo.getRegisterTime() != null ? memberInfo.getRegisterTime() : new Date();
            }

        }
        //2.查询所有通知/活动
        List<TenantNotice> tenantNoticeList = tenantNoticeService.listByMemberIdAndDate(memberId, date);
        if (tenantNoticeList.size() > 0) {
            for (TenantNotice tenantNotice : tenantNoticeList) {
                RcNoticeNotification rcNoticeNotification = new RcNoticeNotification();
                rcNoticeNotification.setTenantCode(memberInfo.getTenantCode());
                rcNoticeNotification.setMsgType(tenantNotice.getNoticeType());
                rcNoticeNotification.setMsgContent(tenantNotice.getTitle());
                rcNoticeNotification.setMemberId(memberId);
                rcNoticeNotification.setCreatedTime(new Date());
                rcNoticeNotification.setIsRead("0");
                rcNoticeNotification.setMsgId(tenantNotice.getNoticeId().toString());
                rcNoticeNotificationMapper.insertSelective(rcNoticeNotification);
            }
            memberInfo.setLastNotityTime(new Date());
            memberInfoService.updateByPrimaryKey(memberInfo);
        }
    }


    @Override
    public void addOrder(Long orderId) {
        //1.查询当前所有用户数据
        OrderInfo orderInfo = orderInfoService.selectOrderById(orderId);
        if (orderInfo == null) {
            return;
        }
        //MemberInfo memberInfo = memberInfoService.selectByPrimaryKey(orderInfo.getMemberId());
        //
        Long memberId = orderInfo.getMemberId();
        //Date date = new Date();
        //if (memberInfo != null) {
        //    date = memberInfo.getLastNotityTime() == null ? memberInfo.getCreatedTime() : memberInfo.getLastNotityTime();
        //    if (date == null) {
        //        date = memberInfo.getRegisterTime() == null ? memberInfo.getRegisterTime() : new Date();
        //    }
        //}
        RcNoticeNotification rcNoticeNotification = new RcNoticeNotification();
        rcNoticeNotification.setTenantCode(orderInfo.getTenantCode());
        rcNoticeNotification.setMsgType("2");
        rcNoticeNotification.setMsgContent("充电结束:" + orderInfo.getOrderNum());
        rcNoticeNotification.setMemberId(memberId);
        rcNoticeNotification.setCreatedTime(new Date());
        rcNoticeNotification.setIsRead("0");
        rcNoticeNotification.setMsgId(orderInfo.getOrderId().toString());
        rcNoticeNotificationMapper.insertSelective(rcNoticeNotification);

    }

    @Override
    public List<RcNoticeNotification> listByMemberId(Long memberId) {

        return rcNoticeNotificationMapper.listByMemberId(memberId);
    }
}
